package com.lingnan.controller;



import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.Admin;
import com.lingnan.pojo.Target;
import com.lingnan.pojo.User;
import com.lingnan.service.ActiveService;
import com.lingnan.service.TargetService;
import com.lingnan.service.UserService;
import com.lingnan.util.HttpRequestUtil;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.AdminVo;
import com.lingnan.vo.AppKey;
import com.lingnan.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ActiveService activeService;

    @Autowired
    private TargetService targetService;

    @Autowired
    private UserService userService;

    @Autowired
    RestTemplate restTemplate;

    @PostMapping("/register")
    @ApiOperation("用户（学生）注册,这个邮箱注册就绑定了")
    public Result register(@ApiParam("传用户对象") @RequestBody UserVo userVo) {
        if (StrUtil.isBlank(userVo.getNo()) || StrUtil.isBlank(userVo.getPassword())) {
            return ResultGenerator.genErrorResult(201, "请输入学号密码！");
        } else {
            User byNo = userService.getByNo(userVo.getNo());
            if (!ObjectUtils.isEmpty(byNo)){
                return ResultGenerator.genErrorResult(201, "该学生已注册！");
            }
            else {
                boolean register = userService.register(userVo);
                //Map map = (Map) (register != null ? register.get("data") : null);
                if (register) {
                    return ResultGenerator.genSuccessResult(200, "注册成功！");
                }
                return ResultGenerator.genSuccessResult(201, "注册失败！");
            }
        }
    }



    @PostMapping("/login")
    @ApiOperation("用户（学生）登录")
    public Result login(@ApiParam("传用户学号和密码") @RequestBody UserVo userVo) {
        String jobId = userVo.getNo();
        String password = userVo.getPassword();
        if (StrUtil.isBlank(jobId) || StrUtil.isBlank(password)) {
            return ResultGenerator.genErrorResult(201, "参数错误！请输入账号密码");
        } else {
//            HashMap<String, String> hashMap = new HashMap<>();
//            hashMap.put("no",userVo.getNo());
//            hashMap.put("password",userVo.getPassword());
//            ResponseEntity<JSONObject> post = HttpRequestUtil.httpRequestByMapAndUrlAndmethod(hashMap, "http://120.77.247.146:1281/student-all-Info-by-id", "get", restTemplate);
//            JSONObject body = post.getBody();
//            HttpStatus statusCode = post.getStatusCode();
//            int value = statusCode.value();
//            Map map = (Map) (body != null ? body.get("data") : null);
//            String no1 = (String) (map != null ? map.get("no") : null);
//            User user = userService.getByNo(no1);
            User user = userService.getByNo(userVo.getNo());
            if (!ObjectUtils.isEmpty(user)){
                User login = userService.login(userVo);
                if (!ObjectUtils.isEmpty(login)) {
                    return ResultGenerator.genSuccessResult(200,login, "登录成功！");
                }
                return ResultGenerator.genSuccessResult(201, "登录失败！密码错误");
            }
            return ResultGenerator.genSuccessResult(202, "该账号没有注册！");
        }
    }

    @PostMapping("/reSetPassword")
    @ApiOperation("用户重置密码,先调用邮箱验证，在q前端验证好，学号，密码一起传回来")
    public Result reSetPassword(@ApiParam("传用户学号,邮箱，和密码") @RequestBody UserVo userVo) {
        if (!ObjectUtils.isEmpty(userVo)){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("no",userVo.getNo());
            queryWrapper.eq("email",userVo.getEmail());
            User one = userService.getOne(queryWrapper);
            if (!ObjectUtils.isEmpty(one)){
                User user = userService.reSetPassword(userVo);
                return ResultGenerator.genSuccessResult(200,user,"用户重置密码成功");
            }
            return ResultGenerator.genSuccessResult(201, "用户信息错误！");
        }
        return ResultGenerator.genSuccessResult(202, "用户重置密码失败！输入选项不能为空");
    }

    @GetMapping("/getEmailCode")
    @ApiOperation("获取邮箱验证")
    public Result getEmailCode(HttpServletRequest request, HttpSession httpSession,@RequestParam("email") String email) {
        if (!ObjectUtils.isEmpty(email)){
            JSONObject jsonObject = userService.getEmailCode(email);
            String emailResult = (String)(jsonObject != null ? jsonObject.get("data") : null);
            //request.setAttribute("code",emailResult);
            httpSession.setAttribute("code",emailResult);
            return ResultGenerator.genSuccessResult(200,emailResult,"获取邮箱验证成功");
        }
        return ResultGenerator.genSuccessResult(201, "获取邮箱验证失败！");
    }


    /**
     * 登录进来主页显示的个人紧急通知
     *
     * @param
     * @return
     */
    @PostMapping("/noticesList")
    @ApiOperation("登录进来主页显示的个人紧急通知")
    public Result noticesList(@ApiParam("传用户no")@RequestBody UserVo userVo) {
        List<Active> actives = null;
        if (!StrUtil.isEmpty(userVo.getNo())){
            String no = userVo.getNo();
            actives = userService.noticesList(no);
        }
        if (ObjectUtils.isNotNull(actives)){
            return ResultGenerator.genSuccessResult(200,actives,"学生的主页显示的个人紧急通知成功");
        }
        return ResultGenerator.genErrorResult(201,"学生的主页显示的个人紧急通知失败,或者请登录后再尝试");
    }

    /**
     * 登录进来主页显示的个人今日活动
     *
     * @param
     * @return
     */
    @PostMapping("/activesTodayList")
    @ApiOperation("登录进来主页显示的个人今日活动")
    public Result activesList(@ApiParam("传用户no")@RequestBody UserVo userVo) {
        List<Active> actives = null;
        String no = userVo.getNo();
        if (!StrUtil.isEmpty(no)){
            actives = userService.activesTodayList(no);
        }
        if (!ObjectUtils.isEmpty(actives)){
            return ResultGenerator.genSuccessResult(200,actives,"学生的主页显示的个人今日活动成功");
        }
        return ResultGenerator.genErrorResult(201,"学生的主页显示的个人今日活动失败,或者请登录后再尝试");
    }

}

